home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / KC3JS4 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  8.4 KB  |  211 lines

  1. package com.sun.java.swing.text;
  2.  
  3. import com.sun.java.swing.event.DocumentEvent;
  4. import java.awt.Color;
  5. import java.awt.Component;
  6. import java.awt.Font;
  7. import java.awt.FontMetrics;
  8. import java.awt.Graphics;
  9. import java.awt.Rectangle;
  10. import java.awt.Shape;
  11.  
  12. public class WrappedPlainView extends BoxView implements TabExpander {
  13.    FontMetrics metrics;
  14.    Segment lineBuffer;
  15.    boolean widthChanging;
  16.    int tabBase;
  17.    int tabSize;
  18.    boolean wordWrap;
  19.    int sel0;
  20.    int sel1;
  21.    Color unselected;
  22.    Color selected;
  23.  
  24.    public WrappedPlainView(Element elem) {
  25.       this(elem, false);
  26.    }
  27.  
  28.    public WrappedPlainView(Element elem, boolean wordWrap) {
  29.       super(elem, 1);
  30.       this.lineBuffer = new Segment();
  31.       this.wordWrap = wordWrap;
  32.    }
  33.  
  34.    protected int calculateBreakPosition(int p0, int p1) {
  35.       this.loadText(p0, p1);
  36.       int p;
  37.       if (this.wordWrap) {
  38.          p = p0 + Utilities.getBreakLocation(this.lineBuffer, this.metrics, this.tabBase, this.tabBase + ((BoxView)this).getWidth(), this, p0);
  39.       } else {
  40.          p = p0 + Utilities.getTabbedTextOffset(this.lineBuffer, this.metrics, this.tabBase, this.tabBase + ((BoxView)this).getWidth(), this, p0);
  41.       }
  42.  
  43.       return p;
  44.    }
  45.  
  46.    public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
  47.       this.updateChildren(e, a);
  48.    }
  49.  
  50.    protected void drawLine(int p0, int p1, Graphics g, int x, int y) {
  51.       try {
  52.          p1 = Math.min(((View)this).getDocument().getLength(), p1);
  53.          if (this.sel0 == this.sel1) {
  54.             this.drawUnselectedText(g, x, y, p0, p1);
  55.          } else if (p0 >= this.sel0 && p0 <= this.sel1 && p1 >= this.sel0 && p1 <= this.sel1) {
  56.             this.drawSelectedText(g, x, y, p0, p1);
  57.          } else if (this.sel0 >= p0 && this.sel0 <= p1) {
  58.             if (this.sel1 >= p0 && this.sel1 <= p1) {
  59.                x = this.drawUnselectedText(g, x, y, p0, this.sel0);
  60.                x = this.drawSelectedText(g, x, y, this.sel0, this.sel1);
  61.                this.drawUnselectedText(g, x, y, this.sel1, p1);
  62.             } else {
  63.                x = this.drawUnselectedText(g, x, y, p0, this.sel0);
  64.                this.drawSelectedText(g, x, y, this.sel0, p1);
  65.             }
  66.          } else if (this.sel1 >= p0 && this.sel1 <= p1) {
  67.             x = this.drawSelectedText(g, x, y, p0, this.sel1);
  68.             this.drawUnselectedText(g, x, y, this.sel1, p1);
  69.          } else {
  70.             this.drawUnselectedText(g, x, y, p0, p1);
  71.          }
  72.  
  73.       } catch (BadLocationException var6) {
  74.          throw new StateInvariantError("Can't render: " + p0 + "," + p1);
  75.       }
  76.    }
  77.  
  78.    protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1) throws BadLocationException {
  79.       g.setColor(this.selected);
  80.       Document doc = ((View)this).getDocument();
  81.       doc.getText(p0, p1 - p0, this.lineBuffer);
  82.       return Utilities.drawTabbedText(this.lineBuffer, x, y, g, this, p0);
  83.    }
  84.  
  85.    protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1) throws BadLocationException {
  86.       g.setColor(this.unselected);
  87.       Document doc = ((View)this).getDocument();
  88.       doc.getText(p0, p1 - p0, this.lineBuffer);
  89.       return Utilities.drawTabbedText(this.lineBuffer, x, y, g, this, p0);
  90.    }
  91.  
  92.    protected final Segment getLineBuffer() {
  93.       return this.lineBuffer;
  94.    }
  95.  
  96.    public float getPreferredSpan(int axis) {
  97.       this.updateMetrics();
  98.       return super.getPreferredSpan(axis);
  99.    }
  100.  
  101.    protected int getTabSize() {
  102.       Integer i = (Integer)((View)this).getDocument().getProperty("tabSize");
  103.       int size = i != null ? i : 8;
  104.       return size;
  105.    }
  106.  
  107.    public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) {
  108.       this.updateChildren(e, a);
  109.       Rectangle alloc = a != null && ((BoxView)this).isAllocationValid() ? ((CompositeView)this).getInsideAllocation(a) : null;
  110.       int pos = e.getOffset();
  111.       View v = ((CompositeView)this).getViewAtPosition(pos, alloc);
  112.       if (v != null) {
  113.          v.insertUpdate(e, alloc, f);
  114.       }
  115.  
  116.    }
  117.  
  118.    protected void loadChildren(ViewFactory f) {
  119.       Element e = ((View)this).getElement();
  120.       int n = e.getElementCount();
  121.       if (n > 0) {
  122.          View[] added = new View[n];
  123.  
  124.          for(int i = 0; i < n; ++i) {
  125.             added[i] = new WrappedLine(this, e.getElement(i));
  126.          }
  127.  
  128.          ((BoxView)this).replace(0, 0, added);
  129.       }
  130.  
  131.    }
  132.  
  133.    final void loadText(int p0, int p1) {
  134.       try {
  135.          Document doc = ((View)this).getDocument();
  136.          doc.getText(p0, p1 - p0, this.lineBuffer);
  137.       } catch (BadLocationException var4) {
  138.          throw new StateInvariantError("Can't get line text");
  139.       }
  140.    }
  141.  
  142.    public float nextTabStop(float x, int tabOffset) {
  143.       int ntabs = ((int)x - this.tabBase) / this.tabSize;
  144.       return (float)(this.tabBase + (ntabs + 1) * this.tabSize);
  145.    }
  146.  
  147.    public void paint(Graphics g, Shape a) {
  148.       Rectangle alloc = (Rectangle)a;
  149.       this.tabBase = alloc.x;
  150.       JTextComponent host = (JTextComponent)((View)this).getContainer();
  151.       this.sel0 = host.getSelectionStart();
  152.       this.sel1 = host.getSelectionEnd();
  153.       this.unselected = ((Component)host).isEnabled() ? ((Component)host).getForeground() : host.getDisabledTextColor();
  154.       Caret c = host.getCaret();
  155.       this.selected = c.isSelectionVisible() ? host.getSelectedTextColor() : this.unselected;
  156.       g.setFont(((Component)host).getFont());
  157.       super.paint(g, a);
  158.    }
  159.  
  160.    public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) {
  161.       this.updateChildren(e, a);
  162.       Rectangle alloc = a != null && ((BoxView)this).isAllocationValid() ? ((CompositeView)this).getInsideAllocation(a) : null;
  163.       int pos = e.getOffset();
  164.       View v = ((CompositeView)this).getViewAtPosition(pos, alloc);
  165.       if (v != null) {
  166.          v.removeUpdate(e, alloc, f);
  167.       }
  168.  
  169.    }
  170.  
  171.    public void setSize(float width, float height) {
  172.       this.updateMetrics();
  173.       if ((int)width != ((BoxView)this).getWidth()) {
  174.          ((BoxView)this).preferenceChanged((View)null, true, true);
  175.          this.widthChanging = true;
  176.       }
  177.  
  178.       super.setSize(width, height);
  179.       this.widthChanging = false;
  180.    }
  181.  
  182.    void updateChildren(DocumentEvent e, Shape a) {
  183.       Element elem = ((View)this).getElement();
  184.       DocumentEvent.ElementChange ec = e.getChange(elem);
  185.       if (ec != null) {
  186.          Element[] removedElems = ec.getChildrenRemoved();
  187.          Element[] addedElems = ec.getChildrenAdded();
  188.          View[] added = new View[addedElems.length];
  189.  
  190.          for(int i = 0; i < addedElems.length; ++i) {
  191.             added[i] = new WrappedLine(this, addedElems[i]);
  192.          }
  193.  
  194.          ((BoxView)this).replace(ec.getIndex(), removedElems.length, added);
  195.          if (a != null) {
  196.             ((BoxView)this).preferenceChanged((View)null, true, true);
  197.             ((View)this).getContainer().repaint();
  198.          }
  199.       }
  200.  
  201.       this.updateMetrics();
  202.    }
  203.  
  204.    final void updateMetrics() {
  205.       Component host = ((View)this).getContainer();
  206.       Font f = host.getFont();
  207.       this.metrics = host.getFontMetrics(f);
  208.       this.tabSize = this.getTabSize() * this.metrics.charWidth('m');
  209.    }
  210. }
  211.